package com.taxi.driver.module.account.modify;

import android.app.Activity;

import com.alibaba.fastjson.JSON;
import com.amap.api.maps.model.LatLng;
import com.qianxx.network.RequestError;
import com.qianxx.utils.RxUtil;
import com.qianxx.utils.encode.RandomUtil;
import com.taxi.driver.R;
import com.taxi.driver.common.BasePresenter;
import com.taxi.driver.config.PositionType;
import com.taxi.driver.data.amap.AMapManager;
import com.taxi.driver.data.user.UserRepository;
import com.taxi.driver.socket.message.UploadLocationMessage;
import com.taxi.driver.socket.message.base.MessageType;

import javax.inject.Inject;

public class PwdModifyPresenter extends BasePresenter implements PwdModifyContract.Presenter {

    private final PwdModifyContract.View mView;
    private final UserRepository mUserRepository;
    private final AMapManager mAMapManager;

    @Inject
    public PwdModifyPresenter(PwdModifyContract.View view, UserRepository userRepository,
                              AMapManager aMapManager) {
        mView = view;
        mUserRepository = userRepository;
        mAMapManager = aMapManager;
    }

    @Override
    public void resetPw(String newPw, String oldPw) {
        mUserRepository.resetPw(RandomUtil.RandomEncrypt(newPw), RandomUtil.RandomEncrypt(oldPw))
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(mView::hideLoadingView)
                .subscribe(driverEntity -> mView.resetSucc(), ex -> {
                    showNetworkError(ex, R.string.network_error, mView, mUserRepository);
                    /* 处理errCode */
                    Throwable throwable = ex.getCause();
                    if (throwable instanceof RequestError) {
                        RequestError error = ((RequestError) throwable);
                        mView.resetFail(error.getErrCode(), error.getMessage());
                    }
                });
    }

    @Override
    public void reqLogout() {
        LatLng latLng = new LatLng(mAMapManager.getLastLocation().getLatitude(), mAMapManager.getLastLocation().getLongitude());
        UploadLocationMessage uploadLocationMessage = new UploadLocationMessage();
        uploadLocationMessage.setAppid(((Activity) mView).getString(R.string.yueyue_appkey));
        uploadLocationMessage.setBizStatus(3);
        uploadLocationMessage.setAreaCode(mAMapManager.getLastLocation().getAdCode());
        uploadLocationMessage.setLat(latLng.latitude);
        uploadLocationMessage.setLng(latLng.longitude);
        uploadLocationMessage.setPositionType(PositionType.SJXB);
        uploadLocationMessage.setDistance(0d);
        uploadLocationMessage.setCarLevelType(mUserRepository.getCarLevelType());
        uploadLocationMessage.setClientUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setDriverUuid(mUserRepository.getLocalDriverUuid());
        uploadLocationMessage.setLocationUuid(System.currentTimeMillis() + "");
        uploadLocationMessage.setType(MessageType.UPLOAD_LOCATION);
        String uploadText = JSON.toJSONString(uploadLocationMessage);

        mUserRepository.reqLogout(latLng.longitude, latLng.latitude, uploadText)
                .compose(RxUtil.applySchedulers())
                .doOnSubscribe(() -> mView.showLoadingView(true))
                .doAfterTerminate(mView::hideLoadingView)
                .subscribe(s -> mView.logoutSuccess(),
                        ex -> showNetworkError(ex, R.string.network_error, mView, mUserRepository));
    }
}
